﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using DTO;

namespace DAO
{
  public   class PhieuDatPhongDAO
    {
      public List<PhieuDatPhongDTO> DanhSachPhieuDatPhong()
      {
          List<PhieuDatPhongDTO> kq = new List<PhieuDatPhongDTO>();
          string str = "select*from PhieuDatPhong";
          DataProvider dp = new DataProvider();
          dp.Connect();
          SqlDataReader dr = dp.ExecuteQuery(str);
          if (dr.HasRows)
          {
              while (dr.Read())
              {
                  PhieuDatPhongDTO pdp = new PhieuDatPhongDTO();
                  pdp.KH_ID = int.Parse(dr["KH_ID"].ToString());
                  pdp.MaPhieuDatPhong = dr["MaPhieuDatPhong"].ToString();
                  pdp.NgayDat = DateTime.Parse(dr["NgayDat"].ToString());
                  pdp.NgayDen = DateTime.Parse(dr["NgayDen"].ToString());
                  pdp.NgayDi = DateTime.Parse(dr["NgayDi"].ToString());
                  pdp.PDP_ID = int.Parse(dr["PDP_ID"].ToString());
                  pdp.SoTienCoc = float.Parse(dr["SoTienCoc"].ToString());
                  pdp.TenDangNhap = dr["TenDangNhap"].ToString();
                  kq.Add(pdp);
              }
          }
          return kq;
      }
      public PhieuDatPhongDTO LayPhieuDatPhongByID(int pDP_ID)
      {
          PhieuDatPhongDTO pdp = new PhieuDatPhongDTO();
          string str = "select*from PhieuDatPhong where PDP_ID=" + pDP_ID;
          DataProvider dp = new DataProvider();
          dp.Connect();
          SqlDataReader dr = dp.ExecuteQuery(str);
          dr.Read();
          pdp.KH_ID = int.Parse(dr["KH_ID"].ToString());
          pdp.MaPhieuDatPhong = dr["MaPhieuDatPhong"].ToString();
          pdp.NgayDat = DateTime.Parse(dr["NgayDat"].ToString());
          pdp.NgayDen = DateTime.Parse(dr["NgayDen"].ToString());
          pdp.NgayDi = DateTime.Parse(dr["NgayDi"].ToString());
          pdp.PDP_ID = int.Parse(dr["PDP_ID"].ToString());
          pdp.SoTienCoc = float.Parse(dr["SoTienCoc"].ToString());
          pdp.TenDangNhap = dr["TenDangNhap"].ToString();
          dp.Disconnect();
          return pdp;
      }
      public PhieuDatPhongDTO LayPhieuDatPhongByMaPhieuDatPhong(string maPhieuDatPhong)
      {
          PhieuDatPhongDTO pdp = new PhieuDatPhongDTO();
          string str = "select*from PhieuDatPhong where PDP_ID='" +maPhieuDatPhong+"'";
          DataProvider dp = new DataProvider();
          dp.Connect();
          SqlDataReader dr = dp.ExecuteQuery(str);
          dr.Read();
          pdp.KH_ID = int.Parse(dr["KH_ID"].ToString());
          pdp.MaPhieuDatPhong = dr["MaPhieuDatPhong"].ToString();
          pdp.NgayDat = DateTime.Parse(dr["NgayDat"].ToString());
          pdp.NgayDen = DateTime.Parse(dr["NgayDen"].ToString());
          pdp.NgayDi = DateTime.Parse(dr["NgayDi"].ToString());
          pdp.PDP_ID = int.Parse(dr["PDP_ID"].ToString());
          pdp.SoTienCoc = float.Parse(dr["SoTienCoc"].ToString());
          pdp.TenDangNhap = dr["TenDangNhap"].ToString();
          dp.Disconnect();
          return pdp;
      }
      public Boolean ThemPhieuDatPhong(PhieuDatPhongDTO pdp)
      {
          Boolean kq = false;
          string str = "insert into PhieuDatPhong(MaPhieuDatPhong,KH_ID,NgayDat,NgayDen,NgayDi,SoTienCoc,TenDangNhap)";
          str += "values('"+pdp.MaPhieuDatPhong+"',"+pdp.KH_ID+",'"+pdp.NgayDat+"','"+pdp.NgayDen+"','"+pdp.NgayDi+"',"+pdp.SoTienCoc+",'"+pdp.TenDangNhap+"')";
          DataProvider dp = new DataProvider();
          dp.Connect();
          int i = dp.ExecuteNonQuery(str);
          if (i != 0)
              kq = true;
          dp.Disconnect();
          return kq;
      }
      public Boolean CapNhatPhieuDatPhong(PhieuDatPhongDTO pdp)
      {
          Boolean kq = false;
          string str = "update PhieuDatPhong set MaPhieuDatPhong='"+pdp.MaPhieuDatPhong+"' and KH_ID="+pdp.KH_ID+" and NgayDat='"+pdp.NgayDat+"' and NgayDen='"+pdp.NgayDen+"' and NgayDi='"+pdp.NgayDi+"' and SoTienCoc="+pdp+" and TenDangNhap='"+pdp.TenDangNhap+"'";
          str += "where PDP_ID=" + pdp.PDP_ID;
          DataProvider dp = new DataProvider();
          dp.Connect();
          int i = dp.ExecuteNonQuery(str);
          if (i != 0)
              kq = true;
          dp.Disconnect();
          return kq;
      }
      public Boolean XoaPhieuDatPhongByID(int pDP_ID)
      {
          Boolean kq = false;
          string str = "delete from PhieuDatPhong where PDP_ID="+pDP_ID;
          DataProvider dp = new DataProvider();
          dp.Connect();
          int i = dp.ExecuteNonQuery(str);
          if (i != 0)
              kq = true;
          dp.Disconnect();
          return kq;
      }
      public Boolean XoaPhieuDatPhongByMaPhieuDatPhong(string maPhieuDatPhong)
      {
          Boolean kq = false;
          string str = "delete from PhieuDatPhong where MaPhieuDatPhong='" + maPhieuDatPhong+"'";
          DataProvider dp = new DataProvider();
          dp.Connect();
          int i = dp.ExecuteNonQuery(str);
          if (i != 0)
              kq = true;
          dp.Disconnect();
          return kq;
      }
      
    }
}
